package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 *  处理图片上传
 */
@Controller
public class UploadController {

    @RequestMapping("/uploadImg")
    @ResponseBody //不贴此注解405(插件返回的数据格式要求json格式)
    private String uploadImg(MultipartFile pic) throws Exception {
        //如何保存图片(MultipartFile pic 参数规定pic)
        String url = UploadUtil.uploadAli(pic);

        return url;//约定返回图片的路径
    }

    //uploadImg_ck

    // 富文本框中的图片添加
    @RequestMapping("/uploadImg_ck")
    @ResponseBody//String module 第二参数路径(没用)
    public Map<String, Object> upload(MultipartFile upload, String module){
        Map<String, Object> map = new HashMap<String, Object>();
        String imagePath= null;
        if(upload != null && upload.getSize() > 0){
            try {
                //图片保存, 返回路径
                imagePath =  UploadUtil.uploadAli(upload);
                //表示保存成功
                map.put("uploaded", 1);
                map.put("url",imagePath);

            }catch (Exception e){
                e.printStackTrace();
                map.put("uploaded", 0);
                Map<String, Object> mm = new HashMap<String, Object>();
                mm.put("message",e.getMessage() );
                map.put("error", mm);
            }
        }
        return map;
    }
}
